package io.renren.modules.store.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.modules.store.entity.TStoreAccountDetailEntity;
import io.renren.modules.store.entity.TStoreAccountUnitEntity;
import io.renren.modules.store.entity.vo.AccountCheckVo;
import io.renren.modules.store.entity.vo.AccountItemList;
import io.renren.modules.store.service.TStoreAccountDetailService;
import io.renren.modules.store.service.TStoreAccountUnitService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.store.entity.TStoreAccountListEntity;
import io.renren.modules.store.service.TStoreAccountListService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-05-13 09:18:17
 */
@RestController
@RequestMapping("store/tstoreaccountlist")
public class TStoreAccountListController extends AbstractController {
    @Autowired
    private TStoreAccountListService tStoreAccountListService;
    @Autowired
    private TStoreAccountDetailService storeAccountDetailService;
    @Autowired
    private TStoreAccountUnitService storeAccountUnitService;

    @RequestMapping("/checkOut")
    @RequiresPermissions("store:tstoreaccountlist:checkOut")
    @NoRepeatSubmit
    public R checkOut(@RequestBody AccountCheckVo accountCheckVo){
        Double totalMoney = 0.0;
        List<TStoreAccountListEntity> storeAccountListEntities = new ArrayList<>();
        for (int i = 0 ; i < accountCheckVo.getCheckOutList().size() ; i++){
            TStoreAccountListEntity storeAccountListEntity = new TStoreAccountListEntity();
            storeAccountListEntity.setId(accountCheckVo.getCheckOutList().get(i).getId())
                    .setStatus(2);
            totalMoney += accountCheckVo.getCheckOutList().get(i).getMoney();
            storeAccountListEntities.add(storeAccountListEntity);
        }
        //更新
        tStoreAccountListService.updateBatchById(storeAccountListEntities);
        //插入结账表
        TStoreAccountDetailEntity storeAccountDetailEntity = new TStoreAccountDetailEntity();
        storeAccountDetailEntity.setAccountUnitId(accountCheckVo.getAccountId())
                .setPayMethod(accountCheckVo.getPayMethod())
                .setCreateTime(new Date())
                .setCreateUser(getUser().getCreateUserId())
                .setSettlementLink(accountCheckVo.getSettlementLink())
                .setSettlementTel(accountCheckVo.getSettlementTel())
                .setTime(new Date())
                .setSettlementMoney(totalMoney);
        storeAccountDetailService.save(storeAccountDetailEntity);
        return R.ok();
    }

    //全部结款
    @RequestMapping("/allCheckOut")
    @RequiresPermissions("store:tstoreaccountlist:allCheckOut")
    @Transactional(rollbackFor = Exception.class)
    @NoRepeatSubmit(lockTime = 5)
    public R allCheckOut(@RequestBody AccountCheckVo accountCheckVo, HttpServletRequest request){
        List<TStoreAccountListEntity> list = tStoreAccountListService.list(new QueryWrapper<TStoreAccountListEntity>()
                .eq("account_id", accountCheckVo.getAccountId())
                .eq("status", 1)
        );
        Double totalMoney = 0.0;
        for (TStoreAccountListEntity tStoreAccountListEntity : list) {
            totalMoney += tStoreAccountListEntity.getMoney();
        }
        accountCheckVo.setSettlementTime(new Date());
        //更新挂单条目表
        tStoreAccountListService.update(new UpdateWrapper<TStoreAccountListEntity>()
            .eq("account_id",accountCheckVo.getAccountId())
                .eq("status",1)
                .set("status",2)
                .set("settlement_link",accountCheckVo.getSettlementLink())
                .set("settlement_tel",accountCheckVo.getSettlementTel())
                .set("settlement_time",accountCheckVo.getSettlementTime())
        );
        //插入结款详情表
        TStoreAccountDetailEntity storeAccountDetailEntity = new TStoreAccountDetailEntity();
        storeAccountDetailEntity.setAccountUnitId(accountCheckVo.getAccountId())
                .setTime(accountCheckVo.getSettlementTime())
                .setSettlementMoney(totalMoney)
                .setSettlementTel(accountCheckVo.getSettlementTel())
                .setCreateUser(getUser().getCreateUserId())
                .setSettlementLink(accountCheckVo.getSettlementLink())
                .setPayMethod(accountCheckVo.getPayMethod())
                .setCreateTime(new Date());
        storeAccountDetailService.save(storeAccountDetailEntity);

        //修改记账总金额
        storeAccountUnitService.updateById(new TStoreAccountUnitEntity()
            .setAccountUnitId(accountCheckVo.getAccountId())
                .setNotSettlementTotal(0.0)
        );
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:tstoreaccountlist:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tStoreAccountListService.queryPage(params);

        return R.ok().put("page", page);
    }




    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("store:tstoreaccountlist:info")
    public R info(@PathVariable("id") Long id){
		TStoreAccountListEntity tStoreAccountList = tStoreAccountListService.getById(id);

        return R.ok().put("tStoreAccountList", tStoreAccountList);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    @RequiresPermissions("store:tstoreaccountlist:save")
    public R save(@RequestBody TStoreAccountListEntity tStoreAccountList){
		tStoreAccountListService.save(tStoreAccountList);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:tstoreaccountlist:update")
    public R update(@RequestBody TStoreAccountListEntity tStoreAccountList){
		tStoreAccountListService.updateById(tStoreAccountList);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:tstoreaccountlist:delete")
    public R delete(@RequestBody Long[] ids){
		tStoreAccountListService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
